package com.crm.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.crm.domain.SysNotice;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.mapper.NoticeMapper;
import com.crm.crm.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PageResult<SysNotice> pageList(Integer status) {
        PageResult<SysNotice> sysNoticePageResult = new PageResult<>();
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>().eq(SysNotice::getStatus, status);
        List<SysNotice> sysNotices = noticeMapper.selectList(wrapper);
        ArrayList<Long> ids = new ArrayList<>();
        if (status == 0) {
            //第一次拉取即已读
            for (SysNotice sysNotice : sysNotices) {
                ids.add(sysNotice.getNoticeId());
            }
            if (ids.size() > 0) {
                updateList(ids);
            }
        }
        sysNoticePageResult.setTotal((long) sysNotices.size());
        sysNoticePageResult.setRows(sysNotices);
        return sysNoticePageResult;
    }

    private void updateList(ArrayList<Long> ids) {
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>().in(SysNotice::getNoticeId, ids);
        List<SysNotice> sysNotices = noticeMapper.selectList(wrapper);
        for (SysNotice sysNotice : sysNotices) {
            sysNotice.setStatus("1");
        }
        noticeMapper.delete(wrapper);
        noticeMapper.insert(sysNotices);
    }

    @Override
    @Transactional
    public boolean update(Long noticeId) {
        SysNotice sysNoticeNew = new SysNotice();
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>().eq(SysNotice::getNoticeId, noticeId);
        SysNotice sysNoticeOld = noticeMapper.selectOne(wrapper);
        BeanUtil.copyProperties(sysNoticeOld, sysNoticeNew);
        sysNoticeNew.setStatus("1");
        try {
            noticeMapper.delete(wrapper);
            noticeMapper.insert(sysNoticeNew);
        } catch (Exception e) {
            log.info("更新读取状态失败：{}", e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public List<SysNotice> selectList(Integer id) {
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>().eq(SysNotice::getStatus, id);
        List<SysNotice> sysNoticesList = noticeMapper.selectList(wrapper);
        return sysNoticesList;
    }

    @Scheduled(cron = "0 0 0 1/1 * * ")
    public void deleteNoticeList() {
        log.info("定时任务deleteNoticeList开始执行，周期为每天执行一次");
        log.info("开始删除消息保留时间多于一周的数据");
        //删除一周前的数据
        Integer limitTime = 7;
        LocalDateTime currentTime = LocalDateTime.now(); // 当前时间
        LocalDateTime lastTime;
        lastTime = currentTime.minus(limitTime, ChronoUnit.DAYS);
        String formattedLastTime = lastTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>().lt(SysNotice::getCreateTime, formattedLastTime);
        int count = noticeMapper.delete(wrapper);
        log.info("执行完毕，共删除{}条数据", count);
    }
}
